package airport.user.action;

import airport.user.dao.UserDAO;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import airport.user.pojo.User;

import com.opensymphony.xwork2.ActionSupport;

public class ChangePasswordAction extends ActionSupport implements
		ServletResponseAware, ServletRequestAware {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String email;
	private String oldPassword;
	private String newPassword;
	private String newPasswordConfirm;
	private String username;

	protected HttpServletResponse servletResponse;

	@Override
	public void setServletResponse(HttpServletResponse servletResponse) {
		this.servletResponse = servletResponse;
	}

	protected HttpServletRequest servletRequest;

	@Override
	public void setServletRequest(HttpServletRequest servletRequest) {
		this.servletRequest = servletRequest;
	}
	
	

	public String execute() {
		
		String ret = "success";
		UserDAO dao = new UserDAO();
		//User user = dao.findUserByUserName(username);
		//user.setPassword(newPassword);
		dao.updatePassword(username, newPassword);
		return ret;

	}

	public void validate() {
		
		Cookie[] cookies = servletRequest.getCookies();
		String cookieName = "AirportUser";
		if (cookies != null) {
			for (int i = 0; i < cookies.length; i++) {
				Cookie cookie = cookies[i];
				if (cookieName.equals(cookie.getName())) {
					username = cookie.getValue();
					break;
				}
			}
		}
		UserDAO dao = new UserDAO();
		User user = dao.findUserByUserName(username);
		String pw = user.getPassword();
		System.out.println("oldpw is: " + pw);
		if (oldPassword == null) {
			addFieldError("oldPassword", "please enter old password");
		} else if (newPassword == null) {
			addFieldError("newPassword", "please enter new password");
		} else if (newPasswordConfirm == null) {
			addFieldError("newPasswordConfirm", "please enter new password confirmation");
		}
		if (newPassword.length() < 8) {
			addFieldError("newPassword",
					"Your Password Needs to Have at least 8 Characters");
		}

		if (!(oldPassword.equals(pw))) {
			addFieldError("oldPassword", "invalid password");
		}
		if (!(newPassword.equals(newPasswordConfirm))) {
			addFieldError("newPasswordConfirm", "confirmation password does not match new password");
		}
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getNewPasswordConfirm() {
		return newPasswordConfirm;
	}

	public void setNewPasswordConfirm(String newPasswordConfirm) {
		this.newPasswordConfirm = newPasswordConfirm;
	}

	public String getNewPassword() {
		return newPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

	public String getOldPassword() {
		return oldPassword;
	}

	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}

}
